package com.actions.bluetoothbox2.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;

import com.actions.bluetoothbox2.R;
import com.actions.bluetoothbox2.app.BrowserActivity;
import com.actions.bluetoothbox2.app.SchoolReportActivity;
import com.actions.bluetoothbox2.util.slideLeftAdapter;
import com.actions.bluetoothbox2.util.slideTitleInfo;
import com.actions.ibluz.manager.BluzManagerData;
import com.actions.ibluz.manager.BluzManagerData.FeatureFlag;
import com.actions.ibluz.manager.IGlobalManager;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by liuyong on 2017/12/18.
 */

public class SlideLeftFragment extends Fragment {

    public static final String FRAGMENT_TAG_MATHER = "mather";
    public static final String FRAGMENT_TAG_TIMETABLE = "timetable";
    public static final String FRAGMENT_TAG_ADDACTION = "addaction";
    public static final String FRAGMENT_TAG_A2DP = "a2dp";
    public static final String FRAGMENT_TAG_CARD = "card";
    public static final String FRAGMENT_TAG_UHOST = "uhost";
    public static final String FRAGMENT_TAG_RADIO = "radio";
    public static final String FRAGMENT_TAG_LINEIN = "linein";
    public static final String FRAGMENT_TAG_ALARM = "alarm";
    public static final String FRAGMENT_TAG_ALARM_SETTING = "alarm_setting";
    public static final String FRAGMENT_TAG_REC_CARDPLAYBACK = "rec_cardplayback";
    public static final String FRAGMENT_TAG_REC_UHOSTPLAYBACK = "rec_uhostplayback";
    public static final String FRAGMENT_TAG_REC_CARD = "rec_card";
    public static final String FRAGMENT_TAG_REC_UHOST = "rec_uhost";
    public static final String FRAGMENT_TAG_USBSOUND = "usb_sound";
    public static final String FRAGMENT_TAG_CONNECTION = "connection";
    public static final String FRAGMENT_TAG_OTA = "ota";
    public static final String FRAGMENT_TAG_CHARGE = "charge";
    public static final String FRAGMENT_TAG_LED = "led";
    public static final String FRAGMENT_TAG_SLEEP = "sleep";
    public static final String FRAGMENT_TAG_ABOUT = "about";
    private static final String TAG = "TAG";
    private static SlideLeftFragment mSlideLeftFragment;
    private BrowserActivity mActivity;
//  int MODE_LEDLAMP    = 14,
//   int MODE_AIDSLEEP   = 15,
//   int MODE_ABOUT      = 22,

    private static int sFeature[] = new int[]{FeatureFlag.SDCARD, FeatureFlag.UHOST, FeatureFlag.FMRADIO, FeatureFlag.LINEIN, FeatureFlag.REC_PLAYBACK,
            FeatureFlag.ALARM, FeatureFlag.FOLDER, FeatureFlag.REC_MIC, FeatureFlag.USBSOUND, FeatureFlag.OTA, 18, 19, 22};

    private List<slideTitleInfo> list;
    private GridView hoem_grid;
    private boolean mSpecialCatalogSelected = false;

//    private List<BluzManagerData.FolderEntry> mFolderEntryList;
//    private IGlobalManager mBluzManager;

    private String mFragmentTag = "";

    private IGlobalManager mBluzManager;
    private List<BluzManagerData.FolderEntry> mFolderEntryList;
    private slideLeftAdapter leftadapter;

    public void setFeatureFilter() {
        mBluzManager = mActivity.getIGlobalManager();
        mFolderEntryList = mBluzManager.getMusicFolderList();
        int listSize = mFolderEntryList.size();
    }

//    public void cardMenuChanged(boolean visibility) {
//        int visible = visibility ? View.VISIBLE : View.GONE;
////        mMusicPlayBtnLayout.setVisibility(visible);
//        list.get(2).visibility = visibility ? 0 : 1;
//
//        boolean recVisible = mBluzManager.isFeatureSupport(BluzManagerData.FeatureFlag.REC_PLAYBACK);
//        boolean folderVisible = mBluzManager.isFeatureSupport(BluzManagerData.FeatureFlag.FOLDER);
//        int recShow = (recVisible && visibility) ? View.VISIBLE : View.GONE;
//        int folderShow = (folderVisible && visibility) ? View.VISIBLE : View.GONE;
////        mCardPlayBackBtnLayout.setVisibility(recShow);
//        list.get(2).visibility = visibility ? 0 : 1;
//
//        if (list.get(2).visibility == 1) {
//            list.get(2).img_id = R.drawable.ic_sd_false;
//        } else {
//            list.get(2).img_id = R.drawable.ic_sd;
//        }
//        leftadapter.notifyDataSetChanged();
////        for (int i = 0; i < mFolderEntryList.size(); i++) {
////            mView[i].setVisibility(folderShow);
////        }
//        boolean cardRecordVisible = mBluzManager.isFeatureSupport(BluzManagerData.FeatureFlag.REC_MIC);
//        int cardRecordShow = (cardRecordVisible && visibility) ? View.VISIBLE : View.GONE;
////        mCardRecordBtnLayout.setVisibility(cardRecordShow);
//    }
//
//    public void uhostMenuChanged(boolean visibility) {
//        int visible = visibility ? View.VISIBLE : View.GONE;
////        mUhostBtnLayout.setVisibility(visible);
//        list.get(3).visibility = visibility ? 0 : 1;
//        if (list.get(3).visibility == 1) {
//            list.get(3).img_id = R.drawable.ic_up_false;
//        } else {
//            list.get(3).img_id = R.drawable.ic_up;
//        }
//        leftadapter.notifyDataSetChanged();
//        boolean recVisible = mBluzManager.isFeatureSupport(BluzManagerData.FeatureFlag.REC_PLAYBACK);
//        int recShow = (recVisible && visibility) ? View.VISIBLE : View.GONE;
////        mUhostPlayBackBtnLayout.setVisibility(recShow);
//        boolean uhostRecordVisible = mBluzManager.isFeatureSupport(BluzManagerData.FeatureFlag.REC_MIC);
//        int uhostRecordShow = (uhostRecordVisible && visibility) ? View.VISIBLE : View.GONE;
////        mUhostRecordBtnLayout.setVisibility(uhostRecordShow);
//    }
//
//    public void lineinMenuChanged(boolean visibility) {
//        int visible = visibility ? View.VISIBLE : View.GONE;
////        mLineinBtnLayout.setVisibility(visible);
//        list.get(4).visibility = visibility ? 0 : 1;
//        if (list.get(4).visibility == 1) {
//            list.get(4).img_id = R.drawable.ic_aux_false;
//        } else {
//            list.get(4).img_id = R.drawable.ic_aux;
//        }
//        leftadapter.notifyDataSetChanged();
//    }
//
//    public void usbSoundMenuChanged(boolean visibility) {
//        int visible = visibility ? View.VISIBLE : View.GONE;
////        mUSBSoundBtnLayout.setVisibility(visible);
//        list.get(5).visibility = visibility ? 0 : 1;
//        if (list.get(5).visibility == 1) {
//            list.get(5).img_id = R.drawable.ic_usb_false;
//        } else {
//            list.get(5).img_id = R.drawable.ic_usb;
//        }
//        leftadapter.notifyDataSetChanged();
//    }

    public SlideLeftFragment() {
        mSlideLeftFragment = this;
    }

    public static SlideLeftFragment getInstance() {
        if (mSlideLeftFragment == null) {
            return new SlideLeftFragment();
        }
        return mSlideLeftFragment;
    }

//    public void setFeatureFilter() {
//        mBluzManager = mActivity.getIGlobalManager();
//        mFolderEntryList = mBluzManager.getMusicFolderList();
//        int listSize = mFolderEntryList.size();
//
//    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity = (BrowserActivity) getActivity();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.slide_left_layout, container, false);
        hoem_grid = (GridView) view.findViewById(R.id.hoem_grid);

        initList();
        leftadapter = new slideLeftAdapter(getActivity(), list);
        hoem_grid.setAdapter(leftadapter);
        hoem_grid.setOnItemClickListener(listener);

        return view;
    }

    AdapterView.OnItemClickListener listener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//            if (position == 0) {
//                if (mActivity.isConnection) {
//                    mFragmentTag = FRAGMENT_TAG_LED;
//                    mActivity.addFragmentToStack(mFragmentTag);
//                    mActivity.mode_now = 14;
//                    mActivity.setMode(14);
//                } else {
//                    mFragmentTag = FRAGMENT_TAG_CONNECTION;
//                    mActivity.addFragmentToStack(mFragmentTag);
//                }
//            } else if (position == 6) {
//                if (mActivity.isConnection) {
//                    mActivity.mode_now = 15;
//                    mFragmentTag = FRAGMENT_TAG_SLEEP;
//                    mActivity.addFragmentToStack(mFragmentTag);
//                    mActivity.setMode(15);
//                } else {
//                    mFragmentTag = FRAGMENT_TAG_CONNECTION;
//                    mActivity.addFragmentToStack(mFragmentTag);
//                }
//            } else {
//
//            }

            int mode = -2;
            switch (position) {
                case 0:
                    mode = 24;
                    break;
                case 1:
                    mode = 23;
                    break;
                case 2:
                    mode = 25;
//                    mFragmentTag = FRAGMENT_TAG_MATHER;
//                    mActivity.replaceFragment(new MatherSelectionFragment(),mFragmentTag);
                    break;
                case 3:
                    Intent intent = new Intent(getActivity(), SchoolReportActivity.class);
                    startActivity(intent);
                    break;
                case 4:
//                    mode = BluzManagerData.FuncMode.LINEIN;
                    mode = 22;
                    break;
//                case 5:
//                    mode = BluzManagerData.FuncMode.USOUND;
//                    break;
//                case 6:
//                    mode = 15;
//                    break;
//                case 7:
//                    mode = BluzManagerData.FuncMode.ALARM;
//                    break;
//                case 8:
//                    mode = 22;
//                    break;
                default:
                    break;
            }
            if (mActivity.isConnection && mode != -2) {
                if (list.get(position).visibility == 0) {
                    mActivity.setMode(mode);
                }
            } else {
//                    mFragmentTag = FRAGMENT_TAG_CONNECTION;
//                    mActivity.addFragmentToStack(mFragmentTag);
            }
        }
    };

    private void initList() {
        list = new ArrayList<>();
        slideTitleInfo info_1 = new slideTitleInfo();
        info_1.img_id = R.drawable.ic_sleep;
        info_1.str = getResources().getString(R.string.appease);
        info_1.id = 1;
        list.add(info_1);

        slideTitleInfo info_2 = new slideTitleInfo();
        info_2.img_id = R.drawable.ic_time_table;
        info_2.str = getResources().getString(R.string.time_table);
        info_2.id = 2;
        list.add(info_2);

        slideTitleInfo info_3 = new slideTitleInfo();
        info_3.img_id = R.drawable.ic_mather;
        info_3.str = getResources().getString(R.string.mather_select);
        info_3.id = 3;
        list.add(info_3);

        slideTitleInfo info_4 = new slideTitleInfo();
        info_4.img_id = R.drawable.ic_school_report;
        info_4.str = getResources().getString(R.string.school_report);
        info_4.id = 3;
        list.add(info_4);

        slideTitleInfo info_9 = new slideTitleInfo();
        info_9.img_id = R.drawable.ic_about;
        info_9.str = getResources().getString(R.string.about);
        info_9.id = 9;
        list.add(info_9);

//        slideTitleInfo info_1 = new slideTitleInfo();
//        info_1.img_id = R.drawable.ic_led;
//        info_1.str = getResources().getString(R.string.led);
//        info_1.id = 1;
//        list.add(info_1);

//        slideTitleInfo info_2 = new slideTitleInfo();
//        info_2.img_id = R.drawable.ic_bluetooth;
//        info_2.str = getResources().getString(R.string.bluetoothpush);
//        info_2.id = 2;
//        list.add(info_2);
//
//        slideTitleInfo info_3 = new slideTitleInfo();
//        info_3.img_id = R.drawable.ic_sd;
//        info_3.str = getResources().getString(R.string.cardsongplay);
//        info_3.id = 3;
//        list.add(info_3);
//
//        slideTitleInfo info_4 = new slideTitleInfo();
//        info_4.img_id = R.drawable.ic_up;
//        info_4.str = getResources().getString(R.string.uhostplay);
//        info_4.id = 4;
//        list.add(info_4);
//
//        slideTitleInfo info_5 = new slideTitleInfo();
//        info_5.img_id = R.drawable.ic_aux;
//        info_5.str = getResources().getString(R.string.linein);
//        info_5.id = 5;
//        list.add(info_5);
//
//        slideTitleInfo info_6 = new slideTitleInfo();
//        info_6.img_id = R.drawable.ic_usb;
//        info_6.str = getResources().getString(R.string.usb_sound);
//        info_6.id = 6;
//        list.add(info_6);
//
//        slideTitleInfo info_7 = new slideTitleInfo();
//        info_7.img_id = R.drawable.ic_sleep;
//        info_7.str = getResources().getString(R.string.sleep);
//        info_7.id = 7;
//        list.add(info_7);
//
//        slideTitleInfo info_8 = new slideTitleInfo();
//        info_8.img_id = R.drawable.ic_alarmclock;
//        info_8.str = getResources().getString(R.string.alarmclock);
//        info_8.id = 8;
//        list.add(info_8);

    }

}
